﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading; 

namespace WindowsGame1.gamePlugins.nxt
{
    class Sending
    {
        private Thread thread;
        private int delay = 100;
        private ControllerBridgeNXT root;

        private Game1.GameState lastState;

        private bool[] lastPowerUps = new bool[7];

        private int[] lastScores = new int[2];

        //private long lastScoreNamed;

        //private bool scoreChange = false;

        //private bool it = false;

        public Sending(ControllerBridgeNXT root)
        {
            this.root = root;
            //this.lastState = root.game.gameState;
            //root.messenger.SendMessage("reseta", root.resetSendMailBox);
            //root.messenger.SendMessage("resetb", root.resetSendMailBox);
            thread = new Thread(new ThreadStart(run));
            thread.Priority = ThreadPriority.AboveNormal;
            thread.Start();
        }

        protected void run() {
            while (root.successfullConnectAndAlive)
            {
                if (root.successfullConnectAndAlive)
                {
                    if (lastState != root.game.gameState)
                    {
                        if (root.game.gameState == Game1.GameState.ScoreScreen)
                        {
                            root.messenger.SendMessage("end", 4);
                        }
                        root.messenger.SendMessage((float) (root.game.gameState == Game1.GameState.InSPGame ? 1 : (root.game.gameState == Game1.GameState.InMPGame ? 2 : (root.game.gameState == Game1.GameState.Calibration ? 3 : 0))), root.multiplayerUpdateMailBox);
                        lastState = root.game.gameState;
                    }
                    //if (((ControllerBridgeNXT.getCurrentTimeMillis() - lastScoreNamed) >= 2500) && scoreChange)
                    //{
                        //root.messenger.SendMessage((float) root.scores[1], root.pointSendMailBox);
                        //root.messenger.SendMessage((float)root.scores[0], root.pointSendMailBox);
                        //scoreChange = false;
                        //lastScoreNamed = ControllerBridgeNXT.getCurrentTimeMillis();
                    //}
                    if (root.game.gameState != Game1.GameState.ScoreScreen)
                    {
                        for (int i = 3; i < root.pu.Length; i++)
                        {
                            if (lastPowerUps[i] != root.pu[i]) //Change detected
                            {
                                if (root.pu[i])
                                {
                                    root.messenger.SendMessage("powerup", root.powerupSendMailBox);
                                }
                                lastPowerUps[i] = root.pu[i];
                                break;
                            }
                        }
                        if (root.batRaaktBal)
                        {
                            root.messenger.SendMessage("collision", root.powerupSendMailBox);
                            root.batRaaktBal = false;
                        }
                        for (int i = 0; i < lastScores.Length; i++)
                        {
                            if (lastScores[i] != root.scores[i]) //Change detected
                            {
                                if (root.scores[i] > 0)
                                {
                                    //root.messenger.SendMessage("punt", root.powerupSendMailBox);
                                    //scoreChange = true;
                                    root.messenger.SendMessage((float)root.scores[1], root.pointSendMailBox);
                                    root.messenger.SendMessage((float)root.scores[0], root.pointSendMailBox);
                                    lastScores[i] = root.scores[i];
                                    break;
                                }
                            }
                        }
                        if (root.game.gameState == Game1.GameState.InSPGame)
                        {

                            /*int betje2Coord_onscreen = (int) root.game.objectsx[2].position.Y;
                            // ball coordinaten = root.ballx en root.bally
                            // coordinaten betje2 = root.betje2
                            // nieuwe coordinaten betje2 = root.bally
                            int newY = root.bally;
                            // bereken verschil met oude coordinaten
                            int diffY = newY - root.betje2;
                            // zet px om naar graden
                            // PX root.maxHeight
                            // GR root.maxdegreescount2
                            double verstuurwaarde = ((double)root.maxdegreescount2 / (double)root.maxHeight) * diffY;
                            Console.WriteLine(verstuurwaarde + " " + diffY + " " + root.maxdegreescount2);
                            Console.WriteLine((double)root.maxdegreescount2 / (double)root.maxHeight);
                            bool blockmotor = true;
                            
                            if (betje2Coord_onscreen == 0 || betje2Coord_onscreen == root.maxHeight)
                            {
                                
                                if (betje2Coord_onscreen == 0)
                                {
                                    if (diffY < 0)
                                    {
                                        diffY = 0;
                                    }
                                    else
                                    {
                                        blockmotor = false;
                                    }
                                }
                                else
                                {
                                    if (diffY > 0)
                                    {
                                        diffY = 0;
                                    }
                                    else
                                    {
                                        blockmotor = false;
                                    }
                                }
                                root.messenger.SendMessage(blockmotor, root.stopMoterMotionMailBox);

                            }
                            else
                            {
                                root.messenger.SendMessage(false, root.stopMoterMotionMailBox);
                            }

                            // verstuur naar NXT
                            if (verstuurwaarde != 0)
                            {
                                Console.WriteLine(verstuurwaarde + " " + newY + " " + diffY + " " + betje2Coord_onscreen);
                                
                                root.messenger.SendMessage((float)verstuurwaarde, root.player2CPUMailBox);
                            }*/
                            
                        }
                    }
                }
                Thread.Sleep(delay);
            }
        }

        public Thread getThread()
        {
            return thread;
        }
    }
}
